package com.ls.service.impl;

import com.ls.dao.CustomerDao;
import com.ls.entity.Customer;
import com.ls.entity.JsonResult;
import com.ls.entity.Page;
import com.ls.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;



@Service("customerService")
public class CustomerServiceImpl implements ICustomerService {
	@Autowired
	CustomerDao dao;

	public JsonResult<List<Customer>> findCustomer() {

		JsonResult<List<Customer>> result = new JsonResult<List<Customer>>();
		List<Customer> list = dao.findCustomer();
		if (list != null) {
			result.setStatus(0);
			result.setData(list);
			return result;
		}
		result.setStatus(1);
		result.setMsg("查询客户出错");
		return result;
	}

	public JsonResult<List<Customer>> findCustomerFenye(int pageId, int num) {
		JsonResult<List<Customer>> result = new JsonResult<List<Customer>>();
		Page page = new Page();
		int jilu = dao.findCustomer().size();
		List<Customer> list = dao.findCustomerFenye((pageId - 1) * num, num);
		if (list != null) {
			page.setPageId(pageId);
			page.setPageNum(jilu % num == 0 ? jilu / num : jilu / num + 1);
			result.setPage(page);
			result.setStatus(0);
			result.setData(list);
			return result;
		}
		result.setStatus(1);
		result.setMsg("查询客户分页出错");
		return result;
	}

	public JsonResult<Object> deleteByIdCs(String[] customer_id) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.deleteByIdCs(customer_id);
		// 失败
		if (i == 0) {
			result.setStatus(1);
			result.setMsg("删除客户失败");
			return result;
		}
		result.setStatus(0);
		result.setData(i);
		return result;
	}

	public JsonResult<List<Customer>> findName(String name, int pageId, int num) {
		JsonResult<List<Customer>> result = new JsonResult<List<Customer>>();
		Page page = new Page();
		int jilu = dao.findName(name).size();
		List<Customer> list = dao.findNameFenye(name, (pageId - 1) * num, num);
		if (list != null) {
			page.setPageId(pageId);
			page.setPageNum(jilu % num == 0 ? jilu / num : jilu / num + 1);
			result.setStatus(0);
			result.setData(list);
			result.setPage(page);
			return result;
		}
		result.setStatus(1);
		result.setMsg("查询失败");
		return result;
	}

	public JsonResult<List<Customer>> findOption() {
		JsonResult<List<Customer>> result = new JsonResult<List<Customer>>();
		List<Customer> list = dao.findOption();
		if (list != null) {
			result.setStatus(0);
			result.setData(list);
			return result;
		}
		result.setStatus(1);
		result.setMsg("查询类customer失败");
		return result;
	}

	public JsonResult<Object> insertCustomer(Customer customer) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.insertCustomer(customer);
		if (i==1) {
			result.setStatus(0);
			result.setData(i);
			return result;
		}
		result.setStatus(1);
		result.setMsg("添加客户失败了");
		return result;
	}

	public JsonResult<Customer> showCu(int customer_id) {
		JsonResult<Customer> result = new JsonResult<Customer>();
		Customer customer = dao.showCu(customer_id);
		if (customer!=null) {
			result.setStatus(0);
			result.setData(customer);
			return result;
		}
		result.setStatus(1);
		result.setMsg("show客户失败了");
		return result;
	}

	public JsonResult<Object> updateCustomer(Customer customer) {
		JsonResult<Object> result = new JsonResult<Object>();
		int i = dao.update(customer);
		if (i==1) {
			result.setStatus(0);
			result.setData(i);
			return result;
		}
		result.setStatus(1);
		result.setMsg("修改客户失败了");
		return result;
	}
}
